package com.qire.manhua.controller;

import android.view.KeyEvent;
import android.view.View;
import android.widget.EditText;

import com.qire.common.support.base.EventController;
import com.qire.manhua.R;
import com.qire.manhua.activity.AccountBindGuideActivity;
import com.qire.manhua.databinding.ActivityAccountBindGuideBinding;
import com.qire.manhua.model.enumeration.AccountBusinessPanelLayout;
import com.qire.manhua.model.viewModel.impl.AccountBindGuideViewModel;

import androidx.annotation.NonNull;

public class AccountBindGuideController extends EventController<AccountBindGuideActivity> {

    private AccountBindGuideViewModel viewModel;

    public AccountBindGuideController(@NonNull AccountBindGuideActivity owner) {
        super(owner);
    }

    @Override
    protected void bindView() {
        ActivityAccountBindGuideBinding viewDataBinder = owner.viewDataBinder();
        viewDataBinder.setAccountBindGuideController(this);
    }

    @Override
    protected void onCleared() {
        super.onCleared();
        this.viewModel = null;
    }

    public void onAddition(AccountBindGuideViewModel viewModel) {
        this.viewModel = viewModel;
    }

    /*************************************************************************************************************************/

    /**退出按钮事件响应*/
    public void backBtnOnClick(View view) {
        owner.finish();
    }

    /**更换手机按钮事件响应*/
    public void changePhoneBtnOnClick(View view) {
        viewModel.nextPage();
    }

    /**旧手机号码验证*/
    public void sendOldPhoneVerifyBtnOnClick(View view) {
        AccountBusinessPanelLayout.CodeVerify.addNext(AccountBusinessPanelLayout.NewPhoneVerify);
        viewModel.setIsArtificialService(false);
        viewModel.setIsNewPhoneVerify(false);
        viewModel.nextPage();
        viewModel.fetchVerificationCode();
    }

    /**新手机号码验证*/
    public void sendNewPhoneVerifyBtnOnClick(View view) {
        if(!viewModel.isArtificialService()) {
            AccountBusinessPanelLayout.CodeVerify.addNext(AccountBusinessPanelLayout.BindGuideFinishHint);
        }
        viewModel.setIsNewPhoneVerify(true);
        viewModel.authNewPhoneNumber();
    }

    /**人工申诉*/
    public void artificialServiceBtnOnClick(View view) {
        viewModel.setIsArtificialService(true);
        AccountBusinessPanelLayout.ChangePhoneByAppeal.addNext(AccountBusinessPanelLayout.Waiting);
        AccountBusinessPanelLayout.CodeVerify.addNext(AccountBusinessPanelLayout.ChangePhoneByAppeal);
        AccountBusinessPanelLayout.NewPhoneVerify.addNext(AccountBusinessPanelLayout.CodeVerify);
        AccountBusinessPanelLayout.OldPhoneVerify.addNext(AccountBusinessPanelLayout.NewPhoneVerify);
        viewModel.gotoPage(AccountBusinessPanelLayout.OldPhoneVerify);
    }
    /**旧手机号码验证*/
    public void oldPhoneVerifyBtnOnClick(View view) {
        viewModel.setIsNewPhoneVerify(false);
        viewModel.authOldPhoneNumber();
    }
    /**提交申诉信息*/
    public void submitAppealInfoBtnOnClick(View view) {
        viewModel.artificialReplaceMobile();
    }

    /**获取验证码*/
    public void fetchVerifyCodeBtnOnClick(View view) {
        viewModel.fetchVerificationCode();
    }

    /**验证码验证按钮事件响应*/
    public void verifyBtnOnClick(View view) {
        if(viewModel.isNewPhoneVerify() && !viewModel.isArtificialService()) {
            viewModel.replaceBindMobile();
        } else {
            viewModel.checkVerificationCode();
        }
    }

    public void verifyCodeOnFocusChange(View view, boolean hasFocus) {
        if(hasFocus) {
            view.setBackgroundResource(R.drawable.app_common_edit_verify_code_n);
        } else {
            view.setBackgroundResource(R.drawable.app_common_edit_verify_code_s);
        }
    }
    //解决光标在最前面的位置导致退格键失效的问题
    public boolean verifyCodeOnKey(View view, int keyCode, KeyEvent event) {
        if(keyCode == KeyEvent.KEYCODE_DEL){
            EditText editText = (EditText) view;
            int textLength = editText.getText().toString().length();
            if(textLength > 0) {
                editText.setSelection(textLength);
            }else{
                editText.setText("");
            }
        }
        return false;
    }

}
